package com.eet.juc.solution;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 8锁问题
 *
 * @author zheng.zhang
 */
public class Test4 {
    private static final ThreadPoolExecutor EXECUTOR_POOL = new ThreadPoolExecutor(3, 5, 1L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) {
        Phone4 phone = new Phone4();
        // 线程1
        EXECUTOR_POOL.submit(phone::sendMessage);

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 线程2
        EXECUTOR_POOL.submit(Phone4::call);
    }
}

/**
 * 手机类
 */
class Phone4 {

    /**
     * 发短信
     */
    public synchronized void sendMessage() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    /**
     * 打电话
     */
    public static synchronized void call() {
        System.out.println("打电话");
    }
}